/**
 * @author migrsoft.com
 */
(function($) {
	$.fn.extend({

		checkboxCtrl : function(parent) {
			return this.each(function() {
				var $trigger = $(this);
				$trigger.click(function() {
					var group = $trigger.attr("group");
					var other = $trigger.attr("other") || "";
					if ($trigger.is(":checkbox")) {
						var type = $trigger.is(":checked") ? "all" : "none";
						if (group)
							$.checkbox.select(group, type, parent, other);
					} else {
						if (group)
							$.checkbox.select(group, $trigger
									.attr("selectType")
									|| "all", undefined, other);
					}
				});
			});
		}
	});

	$.checkbox = {
		selectAll : function(_name, _parent) {
			this.select(_name, "all", _parent);
		},
		unSelectAll : function(_name, _parent) {
			this.select(_name, "none", _parent);
		},
		selectInvert : function(_name, _parent) {
			this.select(_name, "invert", _parent);
		},
		select : function(_name, _type, _parent, other) {
			$parent = $(_parent || document);
			$checkboxLi = $parent.find(":checkbox[name='" + _name + "']"
					+ other);
			switch (_type) {
			case "invert":
				$checkboxLi.each(function() {
					$checkbox = $(this);
					$checkbox.attr('checked', !$checkbox.is(":checked"));
				});
				break;
			case "none":
				$checkboxLi.attr('checked', false);
				break;
			default:
				$checkboxLi.attr('checked', true);
				break;
			}
		}
	};
})(jQuery);
